﻿var EnumManager = function () {
    "use strict"

    var self = this;
    self.EnumName = ko.observable(null)
    self.ItemName = ko.observable(null);
    self.EnumItems = ko.observableArray();


    self.int = function () {
        ko.applyBindings(self, document.getElementById("enumHolder"))
    };

    self.addItemToList = function () {
        var text = self.ItemName();
        if (text !== null && text.trim() !== "") {
            self.EnumItems.push(self.ItemName());
            self.ItemName("");
            document.getElementById("enumItem").focus();
        }
        else {
            alert("Fill the field with the enum item name");
        }
    };

    self.removeItem = function (itemToDelete)
    {
        self.EnumItems.remove(itemToDelete);
    }

    self.saveData = function () {
        if (self.EnumName() != null && self.EnumName().trim().length != 0 && self.EnumItems().length > 0) {
            var parameters = ko.toJSON( { enumName: self.EnumName(), enumItems: self.EnumItems() });
            $.ajax("/LocalManager/CreateNewEnum",
                {
                    contentType: 'application/json; charset=utf-8',
                    data: parameters,
                    type: "POST",
                    cache: "false"
                })
            .done(function (data) {
                $("#editItem").dialog("close");
                toastr.success("New enum created.", "Enum creation");
            })
            .fail(function (data) { alert("error: " + data); });
        }
    };
}